function initEnv() {
	var ajaxbg = $("#background,#progressBar");
	ajaxbg.hide();
	var index;
	$(document).ajaxStart(function() {
		// loading层
		index = layer.load(2, {
			shade: [0.5, '#000']
			// 0.1透明度的白色背景
		});
	}).ajaxStop(function() {
		layer.close(index);
	});

	navTab.init();

	setTimeout(function() {
		initUI();
	}, 10);

}

function initUI(_box) {
	var $p = $(_box || document);

	// ajax
	if($.fn.ajaxTodo) {
		$("a[target=ajaxTodo]", $p).ajaxTodo();
	}
	if($.fn.ajaxLoad) {
		$("a[target=ajaxLoad]", $p).ajaxLoad();
	}

	if($.fn.navTab) {
		$("a[target=navTab]", $p).navTab();
	}

	$("div.sb-pagination", $p).each(function() {
		var $this = $(this);
		$this.pagination({
			targetType: $this.attr("targetType"),
			rel: $p,
			totalCount: $this.attr("totalCount"),
			numPerPage: $this.attr("numPerPage"),
			pageNumShown: $this.attr("pageNumShown") || 5,
			currentPage: $this.attr("currentPage")
		});
	});

	$("div.filePicker", $p).each(function() {
		var $this = $(this);
		if($this.attr('accept') == 'file') {
			$this.uploader({
				accept: {
					title: 'file',
					extensions: 'rar,zip,doc,xls,ppt,docx,xlsx,pptx,pdf,htm,html,txt,xml',
					mimeTypes: '.rar,.zip,.doc,.xls,.docx,.xlsx,.pdf,.htm,.html,.txt,.xml,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-powerpoint,application/vnd.openxmlformats-officedocument.presentationml.presentation'
				},
				fileSingleSizeLimit: 100 * 1024 * 1024
			});
		} else {
			$this.uploader({
				accept: {
					title: 'Images',
					extensions: 'gif,jpg,jpeg,bmp,png',
					mimeTypes: 'image/jpg,image/jpeg,image/png'
				},
				fileSingleSizeLimit: 2 * 1024 * 1024
			});
		}
	});

	// plugin datetimepicker
	if($.fn.datetimepicker) {
		$('input.form_date').datetimepicker({
			language: 'zh-CN',
			format: 'yyyy-mm-dd',
			minView: 2,
			autoclose: true
		});
	}

	$("ul.sb-tabs", $p).each(function() {
		var $this = $(this);
		var $id = $this.attr('data-target');
		var activeItem = $this.attr('data-active');

		$this.children('li').each(function() {
			var $li = $(this);
			var key = $li.attr('data-key');
			if(key == activeItem) {
				$li.addClass('active');
			}
			$li.click(function() {
				$($id).val(key);
				$this.closest('form').submit();
			})
		})
	});

	$("a[target=dialog]", $p).each(function() {
		var $this = $(this);

		$this.click(function() {
			var url = $this.attr('href');
			var title = $this.attr('title') || $this.text();
			var content = '';

			var d = new Date();
			var param = url.indexOf('?') < 0 ? '?v=' + d.getTime() : '&v=' + d.getTime();

			var index = layer.open({
				type: 2,
				title: title,
				content: url + param,
				shadeClose: false,
				scrollbar: false,
				anim: -1,
				area: ['80%', '80%'],
				zIndex: 10
			});

			// layer.full(index);
			//layer.iframeAuto(index);

			event.preventDefault();
		});
	});

	// toastr
	// 参数设置，若用默认值可以省略以下面代
	if(toastr) {
		toastr.options = {
			"closeButton": false, // 是否显示关闭按钮
			"debug": false, // 是否使用debug模式
			"positionClass": "toast-top-center", // 弹出窗的位置
			"showDuration": "300", // 显示的动画时间
			"hideDuration": "1000", // 消失的动画时间
			"timeOut": "5000", // 展现时间
			"extendedTimeOut": "1000", // 加长展示时间
			"showEasing": "swing", // 显示时的动画缓冲方式
			"hideEasing": "linear", // 消失时的动画缓冲方式
			"showMethod": "fadeIn", // 显示时的动画方式
			"hideMethod": "fadeOut" // 消失时的动画方式
		};
	}

	$('input').attr('autocomplete', 'off');
}

$(document).ready(function() {
	initEnv();
})